package com.ac.cv.client;

import java.util.ArrayList;
import java.util.List;

import com.ac.cv.client.menu.MenuItemLabel;
import com.ac.cv.client.service.DataService;
import com.ac.cv.client.service.DataServiceAsync;
import com.ac.cv.client.service.MenuItemDataClient;
import com.ac.cv.client.service.MenuMainItemDataClient;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point class for the whole project
 */
public class CV implements EntryPoint {	  
	
	private DataServiceAsync dataService = GWT.create(DataService.class);
	
	
	public DecoratorPanel getDecoratedWidget(Widget widget) {
		DecoratorPanel result = new DecoratorPanel();
		result.setWidget(widget);
		return result;
	}
	
	public Panel buildMainPanel(Tree sidebar, DeckPanel body){
		HorizontalPanel panel = new HorizontalPanel();
		DockPanel result = new DockPanel();
		result.setSpacing(5);
		result.add(KeyWordsWidget.getInstance().buildKeyWordsSearch(), DockPanel.NORTH);
		Widget w = getDecoratedWidget(sidebar);
		result.add(w, DockPanel.WEST);
		Widget w2 = getDecoratedWidget(body);
		result.add(w2, DockPanel.CENTER);	
		result.add(Log.getInstance().getLabel(), DockPanel.SOUTH);
		Button generateDataBase = new Button("Generate");
		generateDataBase.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				List<MenuMainItemDataClient> listMainItem = new ArrayList<MenuMainItemDataClient>();
				MenuMainItemDataClient mainItem = new MenuMainItemDataClient();
		        mainItem.setTitle("auteur");
		        MenuItemDataClient item1 = new MenuItemDataClient();
		        item1.setTitle("test1");
		        item1.setContent("ceci est le test 1");
		        item1.setParent(mainItem);
		        MenuItemDataClient item2 = new MenuItemDataClient();
		        item1.setTitle("test2");
		        item1.setContent("ceci est le test 2");
		        item1.setParent(mainItem);
		        MenuItemDataClient item3 = new MenuItemDataClient();
		        item1.setTitle("test3");
		        item1.setContent("ceci est le test 3");
		        item1.setParent(mainItem);
		        MenuItemDataClient item4 = new MenuItemDataClient();
		        item1.setTitle("test4");
		        item1.setContent("ceci est le test 4");
		        item1.setParent(mainItem);
		        MenuItemDataClient item5 = new MenuItemDataClient();
		        item1.setTitle("test5");
		        item1.setContent("ceci est le test 5");
		        item1.setParent(mainItem);
		        List<MenuItemDataClient> itemList = new ArrayList<MenuItemDataClient>();
		        itemList.add(item1);
		        itemList.add(item2);
		        itemList.add(item3);
		        itemList.add(item4);
		        itemList.add(item5);
		        
		        mainItem.setItemList(itemList);
		        listMainItem.add(mainItem);
		        dataService.setMenuItemData(item1, new AsyncCallback<Void>(){

					@Override
					public void onFailure(Throwable caught) {
						Log.getInstance().log(caught.toString()
								);
					}

					@Override
					public void onSuccess(Void result) {
						Log.getInstance().log("OK");
					}
		        	
		        });
		        dataService.setMenuMainItemData(listMainItem, new AsyncCallback<Void>(){

					@Override
					public void onFailure(Throwable caught) {
						Log.getInstance().log(caught.toString()
								);
					}

					@Override
					public void onSuccess(Void result) {
						Log.getInstance().log("OK");
					}
		        	
		        });
			}
				
		});
		//result.add(generateDataBase, DockPanel.NORTH);
		
		panel.add(result);
		return panel;
	}
	
	public void initHistoryManage(final Tree tree){
		History.addValueChangeHandler(new ValueChangeHandler<String>() {

			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				List<KeyWord> labelList = KeyWordsWidget.getInstance().getKeyWord(event.getValue());
				if(labelList != null){
					labelList.get(0).onSelect();
				}
			}
		      
		    });

	}
	
	public void onModuleLoad() {
		final Splash splash = new Splash("resources/images/splash.jpg");
		splash.addStyleName("splash");
		splash.center();
		dataService.getMenuMainItemData(new AsyncCallback<List<MenuMainItemDataClient>>() {

			@Override
			public void onFailure(Throwable caught) {
				Log.getInstance().log(caught.getMessage());
			}

			@Override
			public void onSuccess(List<MenuMainItemDataClient> data) {
				DeckPanel body = Body.buildBody();
				final Tree tree = Menu.buildMenu(body, data);
				RootPanel.get("container").add(buildMainPanel(tree, body));
				tree.getItem(0).setState(true);
				tree.setSelectedItem(tree.getItem(0).getChild(0));
				((MenuItemLabel)tree.getItem(0).getChild(0).getWidget()).onSelect();
				initHistoryManage(tree);
				splash.hide();
			}
		
		
		});
		
		
	}
}
